package com.njupt.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.transform.ResultTransformer;

import com.njupt.dao.BaseDao;
import com.njupt.dao.PaginationSupport;
import com.njupt.service.GenericPaginationManager;


public class GenericPaginationManagerImpl<T, PK extends Serializable> implements GenericPaginationManager<T, PK>{

	protected BaseDao baseDao;
	protected Class<T> entityClass;
	public void delete(T entity) {
		baseDao.delete(entity);
	}
	public GenericPaginationManagerImpl(Class entityClass){this.entityClass = entityClass;}

	public List findByHql(String hql, Object... params) {
		return baseDao.executeQuery(hql, params);
	}
	public List find(String hql, Object... params) {
		return baseDao.executeQuery(hql, params);
	}
	public List find(String hql) {
		return baseDao.executeQuery(hql, null);
	}


	public T get(PK id) {
		return (T) baseDao.get(entityClass,id);
	}


	public void remove(PK id) {
		T entity = get(id);
		baseDao.delete(entity);
	}


	public T save(T entity) {
		return (T) baseDao.save(entity);
	}

	public T findUniqueBy(String propertyName, Object value){
		return (T) this.baseDao.findUniqueBy(entityClass,propertyName, value);
	}
	
	public int executeUpdate(String hql, Object... params) {
		return baseDao.executeUpdate(hql, params);
	}

	public List findByCriteria(DetachedCriteria dc) {
		return baseDao.findByCriteria(dc);
	}


	public T queryUniqueResult(String hql, Object... params) {
		return (T) baseDao.queryForUnique(hql, params);
	}

	public T queryUniqueResult(DetachedCriteria detachedCriteria) {
		return (T) baseDao.queryUniqueResultByCriteria(detachedCriteria);
	}
	public int executeUpdateByNativeSql(String nativeSql, Object[] params) {
		return baseDao.executeUpdateByNativeSql(nativeSql, params);
	}

	public PaginationSupport findByExample(Object example, int startIndex,
			int pageSize) {
		return baseDao.findByExample(example, startIndex, pageSize);
	}


	public PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria, int startIndex, int pageSize) {
		return baseDao.findPageByCriteria(detachedCriteria, startIndex, pageSize);
	}


	public PaginationSupport findPageByCriteria(
			DetachedCriteria detachedCriteria, int startIndex, int pageSize,
			ResultTransformer resultTransformer) {
		return baseDao.findPageByCriteria(detachedCriteria, startIndex, pageSize, resultTransformer);
	}


	public PaginationSupport findPageByHql(String hql, int startIndex,
			int pageSize) {
		return baseDao.findPageByHql(hql, startIndex, pageSize, null);
	}


	public PaginationSupport findPageByHql(String hql, int startIndex,
			int pageSize, Object... params) {
		return baseDao.findPageByHql(hql, startIndex, pageSize, params);
	}


	public PaginationSupport findPageByHql(String hql, String countHql,
			int startIndex, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}


	public PaginationSupport findPageByHql(String hql, String vCountHql,
			int startIndex, int pageSize, Object... params) {
		return baseDao.findPageByHql(hql, vCountHql, startIndex, pageSize, params);
	}


	public PaginationSupport paginateByCriteria(
			DetachedCriteria detachedCriteria, int pageNo, int pageSize,
			ResultTransformer resultTransformer) {
		int startIndex = (pageNo-1)*pageSize;
		return baseDao.findPageByCriteria(detachedCriteria, startIndex, pageSize, resultTransformer);
	}


	public PaginationSupport paginateByCriteria(
			DetachedCriteria detachedCriteria, int pageNo, int pageSize) {
		return baseDao.paginateByCriteria(detachedCriteria, pageNo, pageSize);
	}


	public PaginationSupport paginateByHql(String hql, int pageNo,
			int pageSize, Object... params) {
		return baseDao.paginationByHql(hql, pageNo, pageSize, params);
	}


	public PaginationSupport paginationByExample(Object example, int pageNo,
			int pageSize) {
		int startIndex = (pageNo - 1)*pageSize;
		return baseDao.findByExample(example, startIndex, pageSize);
	}


	public BaseDao getBaseDao() {
		return baseDao;
	}


	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}


}

